package com.naiterui.ehp.bs.pharmacist.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.naiterui.ehp.bp.bo.doctor.DrDepartmentBO;
import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bs.pharmacist.entity.Pharmacist;
import com.naiterui.ehp.bs.pharmacist.feign.PharmacistDoctorFeignClient;
import com.naiterui.ehp.bs.pharmacist.repository.mysql.PharmacistRepository;
import com.naiterui.ehp.bs.pharmacist.service.ISuperviseService;
import com.naiterui.ehp.bs.pharmacist.vo.supervise.SupervisePharmacistVO;

import cn.hutool.core.util.IdcardUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author luao
 * @date 2021/6/21 2:08 下午
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class PharmacistSuperviseServiceImpl implements ISuperviseService {

    private final PharmacistRepository pharmacistRepository;

    private final PharmacistDoctorFeignClient doctorFeignClient;

    @Override
    public List<SupervisePharmacistVO> pharmacistList(Date startDate, Date endDate, Long pharmacistId) {
        List<Pharmacist> pharmacists = new ArrayList<>();
        if (pharmacistId == null) {
            pharmacists = pharmacistRepository.findByAuthStatusAndChangedAtBetween(Pharmacist.AUTH_STATUS_PASS,
                startDate, endDate);
        } else {
            Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
            if (pharmacist == null) {
                return null;
            }
            pharmacists.add(pharmacist);
        }
        if (CollectionUtils.isEmpty(pharmacists)) {
            return null;
        }

        // List<Long> pharmacistIds = pharmacists.stream().map(Pharmacist::getId).collect(Collectors.toList());
        // List<RecordUserInfoVO> recordUserInfos = doctorFeignClient.recordUserInfo(pharmacistIds, 2);
        // if (CollectionUtils.isEmpty(recordUserInfos)) {
        // return null;
        // }
        // Map<Long, RecordUserInfoVO> recordUserInfoMap =
        // recordUserInfos.stream().collect(Collectors.toMap(RecordUserInfoVO::getUserId, r -> r));
        List<SupervisePharmacistVO> result = new ArrayList<>();
        DrDepartmentBO department = doctorFeignClient.getDepartment("药学专业");
        for (Pharmacist pharmacist : pharmacists) {
            result.add(SupervisePharmacistVO.builder().id(pharmacist.getId()).name(pharmacist.getName())
                .idCard(pharmacist.getCardNo()).gender(pharmacist.getGender())
                .birthday(DateUtil.parseStrToCustomPatternDate(IdcardUtil.getBirthByIdCard(pharmacist.getCardNo()),
                    DateUtil.DAY_TIME_FORMAT))
                .phone(pharmacist.getPhone()).national(pharmacist.getEthnicity()).departmentId(department.getId())
                .departmentName(department.getName()).title(pharmacist.getTitleId().toString())
                .certIssue(pharmacist.getLicenseDate()).certNumber(pharmacist.getLicenseNo())
                .createdAt(pharmacist.getCreatedAt()).changedAt(pharmacist.getChangedAt()).build());
        }
        return result;
    }
}
